<?php
/**
 * @version SVN: $Id$
 * @package    fmcSeminaire
 * @subpackage Models
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Ludovic Launer {@link }
 * @author     Created on 10-Nov-2010
 * @license    GNU/GPL
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport('joomla.application.component.model');

/**
 * fmcSeminaireList Model
 *
 * @package    fmcSeminaire
 * @subpackage Models
 */
class fmcseminaireanalyseModelFmcseminaireanalyse extends JModel
{
    /**
     * fmcSeminaireList data array
     *
     * @var array
     */
    var $_data;

    /**
     * Retrieves the hello data.
     *
     * @return array Array of objects containing the data from the database
     */
    function getData($campagneId=null)
    {
        $db =& JFactory::getDBO();
		
        $where = '';
        if ($campagneId)
        	$where = ' WHERE d.campagneId=' . $campagneId;
        	
        $query = '
        SELECT
		d.dateId, d.dateDebut, vhs.seminaireId,s.greeting as seminaire, count(vhs.seminaireId) as score
		FROM
		jos_fmcseminaire_dates d JOIN jos_fmcseminaire_vote_has_date vhd on d.dateId=vhd.dateId
		join jos_fmcseminaire_vote_has_seminaire vhs on vhs.voteId=vhd.voteId
		join jos_fmcseminaire s on s.seminaireId=vhs.seminaireId ' .
		$where . '
		group by dateId, seminaireId
		order by dateDebut, score DESC';

        $db->setQuery($query);
        $data = $db->loadObjectList();
		//$data = $db->loadRowList();
        return $data;
    }

function gtAnalyseData($campagneId)
    {
   	$data = $this->getData($campagneId);
   	// --- Compute paging info ---
   	$page 	= JRequest::getVar( 'page' ); 	// Get the requested page 
   	$limit 	= JRequest::getVar( 'rows' ); 	// Get how many rows we want to have into the grid 
   	$count 	= count($data);
    if( $count > 0 ) {
    	$total_pages = ceil($count/$limit);
    } 
    else {
    	$total_pages = 0;
    }
    if ($page > $total_pages) $page=$total_pages;
   	
   	// --- Build response ---
   	$json->page 	= $page;
   	$json->total 	= $total_pages;
    $json->rows 	= $data;
    
    
    return json_encode($json);
    }
    
function gtMiscData($campagneId=null)
    {
    $db =& JFactory::getDBO();
    
    $where = '';
    if ($campagneId)
    	$where = ' WHERE campagneId=' . $campagneId;
        	
	// --- Nb Votes ---
    $query = 'SELECT count(voteId) as value FROM jos_fmcseminaire_votes ' . $where . ';';
    $db->setQuery($query);
    $resp = $db->loadObjectList();
    $data[0] = $resp[0];
    $data[0]->key="Nb Votes";
    
    // --- Last Vote ---
    $query_last_vote= '	SELECT date_format(max(timeStamp), \'%d/%m/%Y %k:%i:%s\') as value
						FROM jos_fmcseminaire_votes ' . $where . ';';
    $db->setQuery($query_last_vote);
    $resp = $db->loadObjectList();
    $data[1] = $resp[0];
    $data[1]->key="Dernier vote";

    
    // --- Build response ---
    $json->rows 	= $data;
    
    return json_encode($json);
    }

function gtCampagneListData()
	{
	$db =& JFactory::getDBO();
	
	// --- List of Campagnes ---
    $query = 'SELECT * FROM jos_fmcseminaire_campagnes ORDER BY campagneId DESC;';
    $db->setQuery($query);
    $resp = $db->loadObjectList();
    
    
    $json = array();
    foreach ($resp as $sem) {
		$json[$sem->campagneId] = $sem->campagneName;    	
    }
    
    return json_encode($json);
	}    
    

}//class
